#include <iostream>

using namespace std;
#include <vector>
#include <algorithm>

class Solution {
public:
    int maxOperations(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int left = 0, right = nums.size() - 1, count = 0;;
        while (left < right)
        {
            if (nums[left] + nums[right] > k) right--;
            else if (nums[left] + nums[right] < k) left++;
            else
            {
                count++;
                right--;
                left++;
            }
        }
        return count;
    }
};